package liuds.music;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import com.scwang.smartrefresh.header.TaurusHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;

import java.util.ArrayList;
import java.util.List;

import liuds.music.model.Music;
import liuds.music.model.User;
import liuds.music.utils.MusicUtils;


/**
 * Created by Administrator on 2018/3/26.
 */

public class App extends Application {

    private static Context context;
    private static List<Activity> activitys = new ArrayList<>();
    public static int screenWidth;
    public static int screenHeight;

    public static SharedPreferences sp;

    public static User USER;

    private final static String SERVER_ADDRESS = "http://192.168.1.122:8080";

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
//        SoundUtils.initSoundPool(this);
        sp = getSharedPreferences("config",MODE_PRIVATE);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;

        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
                return new TaurusHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
    }

    public static void addActivity(Activity activity){
        activitys.add(activity);
    }

    public static void exit(){
        if(activitys.size() > 0){
            for(Activity activity : activitys){
                activity.finish();
            }
            activitys.clear();
        }
        //释放内存，退出程序
//        android.os.Process.killProcess(android.os.Process.myPid());
    }

    public static Context getContext(){
        return context;
    }

    public static void share(Context context, String extraText) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.share));
        intent.putExtra(Intent.EXTRA_TEXT, extraText);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(
                Intent.createChooser(intent, context.getString(R.string.share)));
    }

    public static void log(String text){
        Log.e(context.getResources().getString(R.string.app_name),text);
    }

    public static String getUrl(String url){
        return SERVER_ADDRESS+url;
    }
}
